package vn.appsmobi.ui;

import vn.appsmobi.R;
import vn.appsmobi.activity.DetailActivity;
import vn.appsmobi.adapter.ListAppAdapter;
import vn.appsmobi.loader.BaseLoader;
import vn.appsmobi.loader.DeveloperLoader;
import vn.appsmobi.loader.DeveloperLoader.Result;
import vn.appsmobi.model.AppInfo;
import vn.appsmobi.ulti.Constant;
import vn.appsmobi.ulti.Log;
import vn.appsmobi.widget.EmptyLoadingView;
import vn.appsmobi.widget.PageScrollListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class DeveloperFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<DeveloperLoader.Result>
{
  private static final int LISTAPP_LOADER = 0;
  private ListAppAdapter mListAppAdapter;
  private ListView mListView;
  private EmptyLoadingView mLoadingView;
  private String agency;
  private String agencyid;
  private String mListAppType =  Constant.LISTAPP_TYPE_DEVELOPER;
  
  private AdapterView.OnItemClickListener mItemClickListner = new AdapterView.OnItemClickListener()
  {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
    	if (position < DeveloperFragment.this.mListView.getChildCount());
        {
        	AppInfo appInfo = (AppInfo)DeveloperFragment.this.mListAppAdapter.getItem(position);
        	Intent intent = new Intent(DeveloperFragment.this.getActivity(),DetailActivity.class);
        	intent.putExtra(Constant.INTENT_EXTRA_APP_ID, appInfo.getId());
            intent.putExtra(Constant.INTENT_EXTRA_APP_NAME, appInfo.getName());
            intent.putExtra(Constant.INTENT_EXTRA_APP_CAT, appInfo.getCategory());
            intent.putExtra(Constant.INTENT_EXTRA_APP_SIZE, appInfo.getSize());            
            intent.putExtra(Constant.INTENT_EXTRA_APP_AGENCY, appInfo.getCompanyName());
            intent.putExtra(Constant.INTENT_EXTRA_APP_ICON, appInfo.getIconLink());
            //intent.putExtra(Constant.INTENT_EXTRA_APP_RATE, appInfo.get);
            
            DeveloperFragment.this.getActivity().startActivity(intent);
            DeveloperFragment.this.getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.disappear);
            Log.v("click");
        }
    }
  };
  
  
  public void setListAppType(String type)
  {
      this.mListAppType = type;
  }
  
  public void setAgency(String agency)
  {
      this.agency= agency;
  }
  
  public void setAgencyId(String agencyid)
  {
      this.agencyid = agencyid;
  }
  public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
  {
	  
	DisplayMetrics metrics = new DisplayMetrics();  
	DeveloperFragment.this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);  
	  
    View localView = paramLayoutInflater.inflate(R.layout.listapp_fragment, paramViewGroup, false);
    this.mListView = ((ListView)localView.findViewById(R.id.list_view));
    this.mListAppAdapter = new ListAppAdapter(getActivity(),metrics);
    this.mListView.setAdapter(this.mListAppAdapter);
    this.mListView.setOnItemClickListener(this.mItemClickListner);
    this.mListView.setOnScrollListener(new PageScrollListener(null));
    this.mLoadingView = ((EmptyLoadingView)localView.findViewById(R.id.loading));
    return localView;
  }
  
  public void onActivityCreated(Bundle paramBundle)
  {
    super.onActivityCreated(paramBundle);
    getLoaderManager().initLoader(LISTAPP_LOADER, null, this);
   // initLoader(TopApp_LOADER,null,this,getLoaderManager());
  }
  @Override
  public void onStart(){
      super.onStart();
    
  }
  
  
  
  @Override
  public Loader onCreateLoader(int id, Bundle bundle) {
  	Log.v("onCreateLoader called");
  	if (id == LISTAPP_LOADER)
      {
        this.mLoader = new DeveloperLoader(getActivity());
        ((DeveloperLoader)this.mLoader).setAgency(this.agency);
        ((DeveloperLoader)this.mLoader).setAgencyId(this.agencyid);
        ((DeveloperLoader)this.mLoader).setListAppType(this.mListAppType );
        this.mLoader.setProgressNotifiable(this.mLoadingView);
      }
  	BaseLoader baseLoader = this.mLoader; 
      
     return baseLoader;
  }


  @Override
  public void onLoaderReset(Loader<Result> arg0) {
  	// TODO Auto-generated method stub
  	
  }


@Override
public void onLoadFinished(Loader<Result> loader, Result data) {
	// TODO Auto-generated method stub
	Log.v("onLoadFinished");
	 this.mListAppAdapter.updateData(data.mTopAppInfos);
}


public DeveloperFragment() {
	super();
	// TODO Auto-generated constructor stub
}

  
}


